<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Description of photo
 *
 * @author b4by
 */
class photo extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('model_image');
	}

	public function index($id = '') {
		if (!$id)
			$id = 1;
		$image = $this->model_image->get_info_rule(array(
			'i.id' => $id,
			'suppend' => 0,
				));
		if ($image === FALSE) {
			show_error(lang('image_not_found'));
		}
//		echo time2str($image->upload_date);die;
		$image->views++;
//		if ($image->video == 0) {
//			$this->reCreateThumb2($image->path);
//		}

		$update_data = array();

		$this->load->helper('date');
		$re = $this->config->item('re');
		$image->link = url_create('site', 'photo', 'index', $image->id);
		if (array_key_exists($image->id, $re))
			$image->link = $re[$image->id];
		if ($image->views != 0 && $image->views % $this->siteConfig->counter_update_freq == 0) {
			$stat = fb_get_url_state($image->link);
			if ($stat)
				$update_data = array(
					'likes' => $stat->likes_count,
					'comments' => $stat->comments_count
				);
			$update_data['view_freq'] = $this->siteConfig->counter_update_freq / ((now() - $image->last) / 60);
			$update_data['last'] = now();
		}


		//Update counter
		if (now() - $image->upload_date > $this->siteConfig->time_old * 60 * 60)
			$update_data['new'] = 0;
		$update_data['views'] = $image->views;

		if (count($update_data) > 0) {
			$this->model_image->update($image->id, $update_data);
		}

		//
		/* $viewed = $this->session->userdata('viewed');

		  if (!in_array($image->id, $viewed)) {
		  array_push($viewed, $image->id);
		  $this->session->set_userdata('viewed', $viewed);
		  }

		  $curr_img = array_search($image->id,$viewed);

		  if ($curr_img!==FALSE && $curr_img!=0)
		  $this->data['prevImage'] = $viewed[$curr_img-1];
		  if ($curr_img!==FALSE && $curr_img+1<count($viewed))
		  $this->data['nextImage'] = $viewed[$curr_img+1]; */
		$nextImage = $this->model_image->get_list(array(
			'select' => 'i.id',
			'where' => array(
				'i.id >' => $image->id,
				'suppend' => 0,
				'i.active' => $image->active,
				'video' => $image->video,
				'text_image' => $image->text_image,
			),
			'order' => array('i.id', 'asc'),
			'limit' => array(0, 1)
				)
		);
		$prevImage = $this->model_image->get_list(array(
			'select' => 'i.id',
			'where' => array(
				'i.id <' => $image->id,
				'suppend' => 0,
				'i.active' => $image->active,
				'video' => $image->video,
				'text_image' => $image->text_image,
			),
			'order' => array('i.id', 'desc'),
			'limit' => array(0, 1)
				)
		);

		if (!empty($nextImage))
			$this->data['nextImage'] = $nextImage[0]->id;
		if (!empty($prevImage))
			$this->data['prevImage'] = $prevImage[0]->id;

		$voted = $this->session->userdata('voted');

		if (isset($voted[$image->id])) {
			if ($voted[$image->id] === TRUE)
				$image->upVoted = 1;
			else
				$image->downVoted = 1;
		}
		$uploader = $this->model_user->get_info($image->user_id);
		$this->data['image'] = $image;
		$this->data['image_src'] = image_url($image->thumb2);
		$this->data['uploader'] = $uploader;
		$this->getNewImages();
		$this->getLikeImages();
		$this->getVoteImage();
		$this->data['site_title'] = $image->title; // . ' - ' . $this->data['site_title'];
		$this->data['temp'] = 'photo/index';
		$this->loadView();
	}

	protected function getNewImages() {
		$newImages = $this->model_image->get_list(array(
			'limit' => array(0, 50),
			'order' => array('i.id', 'desc'),
			'where' => array(
				'i.active' => 1,
				'i.suppend' => 0,
			)
				));
		$rand_key = array_rand($newImages, 3);
		$this->data['newImages'] = array();
		foreach ($rand_key as $key) {
			$this->data['newImages'][] = $newImages[$key];
		}
	}

	protected function getLikeImages() {
		if (!$data = $this->cache->file->get('top100freq')) {
			$data = $this->model_image->get_list(array(
				'limit' => array(0, 100),
				'order' => array('view_freq', 'desc'),
				'where' => array(
					'i.active' => 1,
					'i.suppend' => 0,
				)
					));
			$this->cache->file->save('top100freq', $data, 600);
		}
		$rands = array_rand($data, $this->siteConfig->right_new_images);
		$likeImages = array();
		for ($i = 0; $i < $this->siteConfig->right_new_images; $i++) {
			$likeImages[] = $data[$rands[$i]];
		}
		$this->data['likeImages'] = $likeImages;
	}

	protected function getVoteImage() {
		$voteImage = $this->model_image->get_list(array(
			'limit' => array(0, 1),
			'order' => array('i.id', 'RANDOM'),
			'where' => array(
				'new' => 1,
				'i.active' => 0,
				'i.suppend' => 0,
			)
				));
		if (!empty($voteImage))
			$voteImage = $voteImage[0];
		$this->data['voteImage'] = $voteImage;
	}

}

?>
